var ModuleUtil = {
  getFormatMaterialList: function (materialList, classifyData, extraFields) {
    var fields = ['id', 'materialName', 'model', 'material', 'color', 'colorDescription',
      'providerNo', 'defaultWarehouse', 'img', 'imgTwo', 'imgThree', 'imgFour', 'providerName', 'unitPrice',
      'unit', 'company', 'providerName', 'isSynchronization', 'count', 'contactName','materialCode'
    ];

    if (extraFields !== undefined) {
      fields.pushArray(extraFields);
    }

    var self = this;
    materialList = _.map(materialList, function (material) {
      var obj = {};
      SysUtil.setPostData(obj, material, fields, false, true);

      if (obj.unitPrice === 0) {
        obj.unitPrice = '';
      }

      if (obj.count === 0) {
        obj.count = '';
      }

      var status = material.status;
      if (!SysUtil.isEmpty(status)) {
        status = Number(status);
        if (isNaN(status)) {
          obj.status = material.status;
        } else {
          obj.status = status;
        }
      }

      if (classifyData !== undefined) {
        var typeId = material.typeId;
        var classify = classifyData.getTreePath(typeId);
        obj.classify = classify;
        obj.classifyName = _.map(classify, 'name');
      } else {
        obj.classify = [];
        obj.classifyName = [];
      }

      var showImgUrl = self._getShowImgUrl(material);
      obj.showImgUrl = showImgUrl;
      return obj;
    });
    return materialList;
  },

  _getShowImgUrl: function (material) {
    var imgFields = ['img', 'imgTwo', 'imgThree', 'imgFour'];
    var imgUrl = material.showImgUrl;
    if (SysUtil.isEmpty(imgUrl)) {
      for (var i = 0; i < imgFields.length; i++) {
        var imgField = imgFields[i];
        var url = material[imgField];
        if (!SysUtil.isEmpty(url)) {
          imgUrl = url;
          break;
        }
      }
    }
    if (!SysUtil.isEmpty(imgUrl)) {
      if (imgUrl.indexOf('?') === -1) {
        imgUrl += '?imageMogr/auto-orient';
      } else {
        imgUrl += '|imageMogr/auto-orient';
      }
    } else {
      imgUrl = ''; 
    }
    return imgUrl;
  },

  getPurchaseOrderList: function (orderList) {
    var fields = ['providerNo', 'providerName', 'purchasePeople', 'materialName', 'color', 'colorDescription', 'model', 'qualityType',
      'unit', 'price', 'purchaseCount', 'tax', 'purchaseTime', 'createTime', 'company', 'approvalTime'
    ];
    orderList = _.map(orderList, function (order) {
      var obj = {};
      SysUtil.setPostData(obj, order, fields, false, true);

      var purchaseCount = order.purchaseCount;
      var price = order.price;
      obj.totalMoney = _.round(purchaseCount * price, 2);
      return obj;
    });

    return orderList;
  }
};